//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.customer;

import java.awt.event.*;
import java.math.BigDecimal;
import java.util.*;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
import org.jpgolay.gui.fixed.*;
import org.jpgolay.gui.iconBar.*;
import org.jpgolay.me.discount.Discount;
import org.jpgolay.me.discount.PopupDiscountGUI;
import org.jpgolay.me.family.Family;
import org.jpgolay.me.item.Item;
import org.jpgolay.me.item.ItemCRUD;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.salesperson.SalesPerson;
import org.jpgolay.me.util.*;

// Referenced classes of package org.jpgolay.me.customer:
//            CustomerGUI, CustomerCRUD, CustomerPrint, Customer
public class CustomerController extends CustomerGUI {

    private class MouseClickCustomerList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                actionLoad((String) customerTable.getTable().getValueAt(customerTable.getTable().getSelectedRow(), 0));
            }
        }

        MouseClickCustomerList() {
        }
    }

    private class MouseClickItemDiscountList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getButton() != 1 && getItemDiscountTable().isCellEditable()) {
                getPopupDiscount().show(e.getComponent(), e.getX(), e.getY());
            }
        }

        MouseClickItemDiscountList() {
        }
    }

    private class MouseClickFamilyDiscountList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getButton() != 1 && getFamilyItemDiscountTable().isCellEditable()) {
                getPopupDiscount().show(e.getComponent(), e.getX(), e.getY());
            }
        }

        MouseClickFamilyDiscountList() {
        }
    }

    private class MouseClickItemList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2 && getItemDiscountTable().isCellEditable()) {
                getItemDiscountTable().getTable().setValueAt(itemTable.getTable().getValueAt(itemTable.getTable().getSelectedRow(), 0), getItemDiscountTable().getTable().getSelectedRow(), 0);
            }
        }

        MouseClickItemList() {
        }
    }

    public CustomerController(Parameter param) {
        create = false;
        updateRowsTable = true;
        customerTable = null;
        itemTable = null;
        popupDiscount = null;
        write = false;
        this.param = param;
        crud = new ObjectCRUD();
        customerCrud = new CustomerCRUD();
        itemCrud = new ItemCRUD();
        initLaw();
        initMenu();
        initIconBar();
        initField();
        initCustomerFamilyList();
        initSalesPersonList();
        initSearch();
        initCustomerListTable();
        initItemDiscountTable();
        initItemFamilyDiscountTable();
        initPopupDiscountListener();
        initItemSearch();
        initItemListTable();
    }

    private void initLaw() {
        if (param.getAccessLaw().length() >= 15 && param.getAccessLaw().charAt(15) == '1') {
            write = true;
        }
    }

    private void initMenu() {
        getMenuPanel().getExit().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionExit();
            }
        });
        getMenuPanel().getDelete().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionRemove();
            }
        });
        getMenuPanel().getHelp().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionHelp();
            }
        });
        getMenuPanel().getModify().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionModify();
            }
        });
        getMenuPanel().getNew().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionNew();
            }
        });
        getMenuPanel().getOpen().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionevent) {
            }
        });
        getMenuPanel().getPrint().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionPrint(true);
            }
        });
        getMenuPanel().getSave().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSave();
            }
        });
    }

    private void initIconBar() {
        getIconBarPanel().addAnswerListener(new AnswerListener() {
            public void create(AnswerEvent e) {
                actionNew();
            }

            public void open(AnswerEvent answerevent) {
            }

            public void modify(AnswerEvent e) {
                actionModify();
            }

            public void save(AnswerEvent e) {
                actionSave();
            }

            public void remove(AnswerEvent e) {
                actionRemove();
            }

            public void print(AnswerEvent e) {
                actionPrint(false);
            }

            public void help(AnswerEvent e) {
                actionHelp();
            }
        });
    }

    private void initField() {
        cleanField();
        setEnabledField(false);
        getIconBarPanel().setEnabledCreate(write);
        getIconBarPanel().setEnabledSave(false);
        getIconBarPanel().setEnabledModify(false);
        getIconBarPanel().setEnabledRemove(false);
        getIconBarPanel().setEnabledPrint(false);
        getIconBarPanel().setEnabledOpen(false);
        getMenuPanel().getNew().setEnabled(write);
        getMenuPanel().getSave().setEnabled(false);
        getMenuPanel().getModify().setEnabled(false);
        getMenuPanel().getDelete().setEnabled(false);
        getMenuPanel().getPrint().setEnabled(false);
        getMenuPanel().getOpen().setEnabled(false);
        create = false;
        itemFamilyDiscountList = null;
        itemDiscountList = null;
    }

    private void initCustomerFamilyList() {
        java.util.Collection c = crud.readAll(null, 46);
        if (c != null) {
            Family family = null;
            ArrayList familyList = new ArrayList(c);
            Iterator it = familyList.iterator();
            String list[] = new String[familyList.size()];
            for (int i = 0; it.hasNext(); i++) {
                family = (Family) it.next();
                list[i] = family.getId();
            }

            setFamilyList(list);
        } else {
            setFamilyList(new String[]{
                        "aucune"
                    });
        }
    }

    private void initSalesPersonList() {
        java.util.Collection c = crud.readAll(null, 15);
        if (c != null) {
            SalesPerson salesPerson = null;
            salesPersonList = new ArrayList(c);
            Iterator it = salesPersonList.iterator();
            String list[] = new String[salesPersonList.size()];
            salesPersonIdList = new HashMap();
            for (int i = 0; it.hasNext(); i++) {
                salesPerson = (SalesPerson) it.next();
                salesPersonIdList.put(salesPerson.getId(), new Integer(i));
                list[i] = salesPerson.getPrenom() + " " + salesPerson.getNom();
            }

            setSalesPersonList(list);
        } else {
            setSalesPersonList(new String[]{
                        "aucun"
                    });
        }
    }

    private void initItemDiscountTable() {
        getItemDiscountTable().setTypes(new Class[]{
                    java.lang.String.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.math.BigDecimal.class
                });
        getItemDiscountTable().setLastColumnNotEditable(-1);
        getItemDiscountTable().setCellEditable(false);
        getItemDiscountTable().setAlternateColor(MEUtility.ITEM_DISCOUNT_LINE);
        getItemDiscountTable().setColumnNames(new String[]{
                    "REF", "R", "Q1", "R1", "Q2", "R2", "Q3", "R3"
                });
        getItemDiscountTable().setPreferredColumnSize(new int[]{
                    50, 30, 30, 30, 30, 30, 30, 30
                });
        getItemDiscountTable().setData(getDiscountEmptyTable());
    }

    private void initItemFamilyDiscountTable() {
        getFamilyItemDiscountTable().setTypes(new Class[]{
                    java.lang.String.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.math.BigDecimal.class
                });
        getFamilyItemDiscountTable().setLastColumnNotEditable(-1);
        getFamilyItemDiscountTable().setCellEditable(false);
        getFamilyItemDiscountTable().setAlternateColor(MEUtility.ITEM_FAMILY_DISCOUNT_LINE);
        getFamilyItemDiscountTable().setColumnNames(new String[]{
                    "REF", "R", "Q1", "R1", "Q2", "R2", "Q3", "R3"
                });
        getFamilyItemDiscountTable().setPreferredColumnSize(new int[]{
                    50, 30, 30, 30, 30, 30, 30, 30
                });
        getFamilyItemDiscountTable().setData(getDiscountEmptyTable());
    }

    private Object[][] getDiscountEmptyTable() {
        Object data[][] = new Object[3][8];
        for (int i = 0; i < data.length; i++) {
            if (i == 1) {
                data[i][0] = "AUCUNE";
            } else {
                data[i][0] = null;
            }
            data[i][1] = null;
            data[i][2] = null;
            data[i][3] = null;
            data[i][4] = null;
            data[i][5] = null;
            data[i][6] = null;
            data[i][7] = null;
        }

        return data;
    }

    private void initSearch() {
        getCustomerSearchBox().setList(new String[]{
                    "Code", "Raison social", "Ville", "Commercial (nom)", "Commercial (code)", "Famille"
                });
        getCustomerSearchBox().setVal("");
        getCustomerSearchBox().setAdvanceSearch(getCustomerTable());
        getCustomerSearchBox().getButtonRight().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSearch();
            }
        });
        getCustomerSearchBox().getTextField().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSearch();
            }
        });
    }

    private void initCustomerListTable() {
        customerTable.setTypes(new Class[]{
                    java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
                });
        customerTable.setLastColumnNotEditable(0);
        customerTable.setCellEditable(false);
        customerTable.setAlternateColor(MEUtility.CUSTOMER_LINE);
        customerTable.setColumnNames(new String[]{
                    "CODE", "CLIENT", "VILLE", "FAMILLE", "COMMERCIAL"
                });
        customerTable.setPreferredColumnSize(new int[]{
                    40, 50, 40, 50, 30
                });
        actionSearch();
    }

    private void initItemSearch() {
        getItemSearchBox().setList(new String[]{
                    "R\351f\351rence", "D\351signation", "Famille", "Fournisseur"
                });
        getItemSearchBox().setVal("");
        getItemSearchBox().setAdvanceSearch(getItemTable());
        getItemSearchBox().getButtonRight().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionItemSearch();
            }
        });
        getItemSearchBox().getTextField().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionItemSearch();
            }
        });
    }

    private void initItemListTable() {
        itemTable.setTypes(new Class[]{
                    java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
                });
        itemTable.setLastColumnNotEditable(0);
        itemTable.setCellEditable(false);
        itemTable.setAlternateColor(MEUtility.ITEM_LINE);
        itemTable.setColumnNames(new String[]{
                    "REFERENCE", "DESIGNATION", "FAMILLE", "FOURNISSEUR"
                });
        itemTable.setPreferredColumnSize(new int[]{
                    50, 50, 40, 40
                });
        actionItemSearch();
    }

    private TableViewer getCustomerTable() {
        if (customerTable == null) {
            customerTable = new TableViewer();
        }
        return customerTable;
    }

    private TableViewer getItemTable() {
        if (itemTable == null) {
            itemTable = new TableViewer();
        }
        return itemTable;
    }

    private PopupDiscountGUI getPopupDiscount() {
        if (popupDiscount == null) {
            popupDiscount = new PopupDiscountGUI();
        }
        return popupDiscount;
    }

    private void initPopupDiscountListener() {
        getPopupDiscount().getDeleteLine().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionDeleteDiscountLine();
            }
        });
    }

    private void actionExit() {
        setVisible(false);
    }

    private void actionRemove() {
        int reponse = JOptionPane.showConfirmDialog(null, "\nEtes-vous certain de vouloir suprimer ce Client ?\n\nCette op\351ration est fortement d\351conseill\351e par\nl'\351diteur de JGestcom, car les statistiques\nde vente et la vue de votre balance comptable\nserat d\351finitivement fauss\351e.\n\n<html><b><font color=red>Attention il est imp\351ratif d'imprim\351 une copie</font></b></html>\n<html><b><font color=red>de toutes les factures que vous avez \351dit\351e pour</font></b></html>\n<html><b><font color=red>ce client et de les concerver avant de supprimer</font></b></html>\n<html><b><font color=red>d\351finitivement toutes traces de ce client dans la</font></b></html>\n<html><b><font color=red>base de donn\351e !</font></b></html>\n<html><b><font color=red>Si vous ne respecter pas cette consigne vous</font></b></html>\n<html><b><font color=red>d\351cider de votre plein gr\351 de violer les lois en</font></b></html>\n<html><b><font color=red>vigueur et d'en assumer toutes les cons\351quences.</font></b></html>\n\n", "Confirmation", 1, 2, null);
        switch (reponse) {
            case 0: // '\0'
                actionDelete();
            // fall through

            case -1:
            case 1: // '\001'
            case 2: // '\002'
            default:
                return;
        }
    }

    private void actionHelp() {
        BrowserControl.displayURL("www.media-4-u.net");
    }

    private void actionModify() {
        setEnabledField(true);
        getIconBarPanel().setEnabledCreate(false);
        getIconBarPanel().setEnabledSave(true);
        getIconBarPanel().setEnabledModify(false);
        getIconBarPanel().setEnabledRemove(true);
        getMenuPanel().getNew().setEnabled(false);
        getMenuPanel().getSave().setEnabled(true);
        getMenuPanel().getModify().setEnabled(false);
        getMenuPanel().getDelete().setEnabled(true);
        getFamilyItemDiscountTable().setCellEditable(true);
        getItemDiscountTable().setCellEditable(true);
    }

    private void actionNew() {
        initField();
        setEnabledField(true);
        getIconBarPanel().setEnabledCreate(false);
        getIconBarPanel().setEnabledSave(true);
        getMenuPanel().getNew().setEnabled(false);
        getMenuPanel().getSave().setEnabled(true);
        create = true;
        getFamilyItemDiscountTable().setCellEditable(true);
        getItemDiscountTable().setCellEditable(true);
    }

    private void actionPrint(boolean b) {
        CustomerPrint prt = new CustomerPrint(customer, getFamilyItemDiscountTable().getData(), getItemDiscountTable().getData(), true, param);
    }

    private void actionSave() {
        if (create) {
            actionCreate();
        } else {
            actionUpdate();
        }
    }

    private Object[][] getPiecesList() {
        Object data[][] = customerCrud.getDataList("SELECT id, date, recu_le, (net_a_payer - montant_regle), net_a_payer FROM achats WHERE clientss_id='" + customer.getId() + "'");
        if (data == null) {
            return new Object[1][5];
        } else {
            return data;
        }
    }

    private void setFamilyColumn(JTable table, TableColumn familyColumn) {
        JComboBox comboBox = new JComboBox();
        java.util.Collection c = crud.readAll(null, 12);
        if (c != null) {
            ArrayList itemFamilyList = new ArrayList(c);
            Family family;
            for (Iterator it = itemFamilyList.iterator(); it.hasNext(); comboBox.addItem(family.getId())) {
                family = (Family) it.next();
            }

        } else {
            comboBox.addItem("");
        }
        familyColumn.setCellEditor(new DefaultCellEditor(comboBox));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("S\351l\351ctionner une famille");
        familyColumn.setCellRenderer(renderer);
    }

    private void actionSearch() {
        StringBuffer sql = new StringBuffer();
        if (getCustomerSearchBox().getSelectedIndex() == 3) {
            sql.append("SELECT clients.id, raison_sociale, ville_fact, familles_clients_id, nom FROM clients, commerciaux");
        } else {
            sql.append("SELECT id, raison_sociale, ville_fact, familles_clients_id, commerciaux_id FROM clients");
        }
        switch (getCustomerSearchBox().getSelectedIndex()) {
            case 0: // '\0'
                if (getCustomerSearchBox().getVal().equals("")) {
                    sql.append(" ORDER BY id");
                } else {
                    sql.append(" WHERE id LIKE '");
                    sql.append(getCustomerSearchBox().getVal());
                    sql.append("%' ORDER BY id");
                }
                break;

            case 1: // '\001'
                if (getCustomerSearchBox().getVal().equals("")) {
                    sql.append(" ORDER BY raison_sociale");
                } else {
                    sql.append(" WHERE raison_sociale LIKE '");
                    sql.append(getCustomerSearchBox().getVal());
                    sql.append("%' ORDER BY raison_sociale");
                }
                break;

            case 2: // '\002'
                if (getCustomerSearchBox().getVal().equals("")) {
                    sql.append(" ORDER BY ville_fact");
                } else {
                    sql.append(" WHERE ville_fact LIKE '");
                    sql.append(getCustomerSearchBox().getVal());
                    sql.append("%' ORDER BY ville_fact");
                }
                break;

            case 3: // '\003'
                if (getCustomerSearchBox().getVal().equals("")) {
                    sql.append(" WHERE commerciaux_id=commerciaux.id");
                    sql.append(" ORDER BY nom");
                } else {
                    sql.append(" WHERE commerciaux_id=commerciaux.id");
                    sql.append(" AND nom LIKE '");
                    sql.append(getCustomerSearchBox().getVal());
                    sql.append("%' ORDER BY nom");
                }
                break;

            case 4: // '\004'
                if (getCustomerSearchBox().getVal().equals("")) {
                    sql.append(" ORDER BY commerciaux_id");
                } else {
                    sql.append(" WHERE commerciaux_id LIKE '");
                    sql.append(getCustomerSearchBox().getVal());
                    sql.append("%' ORDER BY commerciaux_id");
                }
                break;

            case 5: // '\005'
                if (getCustomerSearchBox().getVal().equals("")) {
                    sql.append(" ORDER BY familles_clients_id");
                } else {
                    sql.append(" WHERE familles_clients_id LIKE '");
                    sql.append(getCustomerSearchBox().getVal());
                    sql.append("%' ORDER BY familles_clients_id");
                }
                break;

            default:
                if (getCustomerSearchBox().getVal().equals("")) {
                    sql.append(" ORDER BY id");
                } else {
                    sql.append(" WHERE id LIKE '");
                    sql.append(getCustomerSearchBox().getVal());
                    sql.append("%' ORDER BY id");
                }
                break;
        }
        Object data[][] = customerCrud.getDataList(sql.toString());
        if (data != null) {
            customerTable.setData(data);
            customerTable.getTable().addMouseListener(new MouseClickCustomerList());
        } else {
            customerTable.setData(getEmptyTable());
        }
    }

    private Object[][] getEmptyTable() {
        Object data[][] = new Object[3][5];
        for (int i = 0; i < data.length; i++) {
            data[i][0] = null;
            if (i == 1) {
                data[i][1] = "AUCUN";
                data[i][2] = "CLIENT";
            } else {
                data[i][1] = null;
                data[i][2] = null;
            }
            data[i][3] = null;
            data[i][4] = null;
        }

        return data;
    }

    private void actionItemSearch() {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT ref_code_barre, designation, familles_articles_id, fournisseurs_id FROM articles");
        switch (getItemSearchBox().getSelectedIndex()) {
            case 0: // '\0'
                if (getItemSearchBox().getVal().equals("")) {
                    sql.append(" ORDER BY ref_code_barre");
                } else {
                    sql.append(" WHERE ref_code_barre LIKE '");
                    sql.append(getItemSearchBox().getVal());
                    sql.append("%' ORDER BY ref_code_barre");
                }
                break;

            case 1: // '\001'
                if (getItemSearchBox().getVal().equals("")) {
                    sql.append(" ORDER BY designation");
                } else {
                    sql.append(" WHERE designation LIKE '");
                    sql.append(getItemSearchBox().getVal());
                    sql.append("%' ORDER BY designation");
                }
                break;

            case 2: // '\002'
                if (getItemSearchBox().getVal().equals("")) {
                    sql.append(" ORDER BY familles_articles_id");
                } else {
                    sql.append(" WHERE familles_articles_id LIKE '");
                    sql.append(getItemSearchBox().getVal());
                    sql.append("%' ORDER BY familles_articles_id");
                }
                break;

            case 3: // '\003'
                if (getItemSearchBox().getVal().equals("")) {
                    sql.append(" ORDER BY fournisseurs_id");
                } else {
                    sql.append(" WHERE fournisseurs_id LIKE '");
                    sql.append(getItemSearchBox().getVal());
                    sql.append("%' ORDER BY fournisseurs_id");
                }
                break;

            default:
                if (getItemSearchBox().getVal().equals("")) {
                    sql.append(" ORDER BY ref_code_barre");
                } else {
                    sql.append(" WHERE ref_code_barre LIKE '");
                    sql.append(getItemSearchBox().getVal());
                    sql.append("%' ORDER BY ref_code_barre");
                }
                break;
        }
        Object data[][] = itemCrud.getDataList(sql.toString());
        if (data != null) {
            itemTable.setData(data);
            itemTable.getTable().addMouseListener(new MouseClickItemList());
        } else {
            itemTable.setData(getEmptyItemTable());
        }
    }

    private Object[][] getEmptyItemTable() {
        Object data[][] = new Object[3][4];
        for (int i = 0; i < data.length; i++) {
            data[i][0] = null;
            if (i == 1) {
                data[i][1] = "AUCUN";
                data[i][2] = "ARTICLE";
            } else {
                data[i][1] = null;
                data[i][2] = null;
            }
            data[i][3] = null;
        }

        return data;
    }

    private void actionLoad(String pk) {
        customer = (Customer) crud.read(pk, 27);
        if (customer != null) {
            initField();
            setCompanyValue(customer.getRaisonSociale());
            setStatusValue(customer.getCivilite());
            setSalesAddrLn1Value(customer.getAdresseFactUn());
            setSalesAddrLn2Value(customer.getAdresseFactDeux());
            setSalesCityValue(customer.getVilleFact());
            setSalesCountryValue(customer.getPaysFact());
            setSalesZipValue(customer.getCpFact());
            setContactValue(customer.getNomPrenomContact());
            setJobContactValue(customer.getFonctionContact());
            setTelValue(customer.getTelephone());
            setFaxValue(customer.getFax());
            setShippingAddrLn1Value(customer.getAdresseLivrUn());
            setShippingAddrLn2Value(customer.getAdresseLivrDeux());
            setShippingCityValue(customer.getVilleLivr());
            setShippingZipValue(customer.getCpLivr());
            setShippingCountryValue(customer.getPaysLivr());
            setGlnValue(customer.getIdGLN());
            setEmailValue(customer.getEmail());
            setWebValue(customer.getSiteWeb());
            setTel2Value(customer.getPortable());
            setFamilyValue(customer.getFamilleClientId());
            setBankNumberValue(customer.getNumeroCompte());
            setLastUserValue(customer.getUserId());
            setLastTimeValue(customer.getUpdateLastTime());
            setIdValue(customer.getId());
            setNotesValue(customer.getCommentaire());
            if (customer.getTotalEncaisser() != null) {
                setTotalPayment(customer.getTotalEncaisser().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
            }
            if (customer.getCotaFidelite() != null) {
                setExtraDiscountLevel(customer.getCotaFidelite().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
            }
            if (customer.getRemiseFidelite() != null) {
                setExtraDiscount(customer.getRemiseFidelite().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
            }
            if (customer.getRemise() != null) {
                setDiscount(customer.getRemise().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString());
            }
            if (customer.getCommerciauxId() != null && customer.getCommerciauxId().intValue() != 0) {
                setSalesPersonIndex(((Integer) salesPersonIdList.get(customer.getCommerciauxId())).intValue());
            }
            if (customer.getEcheancePaiement() != null) {
                setPaymentDelay(customer.getEcheancePaiement().setScale(0).toString());
            }
            if (customer.getModeReglement() != null) {
                setPaymentMode(customer.getModeReglement());
            }
            setTitle("CLIENT (CODE : " + customer.getId() + ")");
            getIconBarPanel().setEnabledModify(write);
            getIconBarPanel().setEnabledPrint(true);
            getMenuPanel().getModify().setEnabled(write);
            getMenuPanel().getPrint().setEnabled(true);
            getFamilyItemDiscountTable().setCellEditable(false);
            getItemDiscountTable().setCellEditable(false);
            create = false;
            actionLoadItemDiscount();
            actionLoadItemFamilyDiscount();
        }
    }

    private void actionLoadItemDiscount() {
        java.util.Collection c = crud.readAll(customer.getId(), 48);
        if (c != null) {
            itemDiscountList = new ArrayList(c);
            Object data[][] = new Object[itemDiscountList.size() + 5][8];
            Iterator it = itemDiscountList.iterator();
            for (int i = 0; it.hasNext(); i++) {
                Discount d = (Discount) it.next();
                data[i][0] = d.getArticleId();
                data[i][1] = d.getRemise().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][2] = d.getQtUn().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][3] = d.getRemiseQtUn().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][4] = d.getQtDeux().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][5] = d.getRemiseQtDeux().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][6] = d.getQtTrois().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][7] = d.getRemiseQtTrois().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
            }

            for (int i = itemDiscountList.size(); i < itemDiscountList.size() + 5; i++) {
                data[i][0] = null;
                data[i][1] = null;
                data[i][2] = null;
                data[i][3] = null;
                data[i][4] = null;
                data[i][5] = null;
                data[i][6] = null;
                data[i][7] = null;
            }

            getItemDiscountTable().setData(data);
        } else {
            itemDiscountList = null;
            getItemDiscountTable().setData(getDiscountEmptyTable());
        }
        getItemDiscountTable().getTable().addMouseListener(new MouseClickItemDiscountList());
        getItemDiscountTable().getTableModel().addTableModelListener(new TableModelListener() {
            public void tableChanged(TableModelEvent e) {
                if (updateRowsTable) {
                    actionUpdateItemDiscountRow(e.getFirstRow(), e.getColumn());
                }
            }
        });
    }

    private void actionLoadItemFamilyDiscount() {
        java.util.Collection c = crud.readAll(customer.getId(), 49);
        if (c != null) {
            itemFamilyDiscountList = new ArrayList(c);
            Object data[][] = new Object[itemFamilyDiscountList.size() + 5][8];
            Iterator it = itemFamilyDiscountList.iterator();
            for (int i = 0; it.hasNext(); i++) {
                Discount d = (Discount) it.next();
                data[i][0] = d.getArticleId();
                data[i][1] = d.getRemise().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][2] = d.getQtUn().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][3] = d.getRemiseQtUn().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][4] = d.getQtDeux().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][5] = d.getRemiseQtDeux().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][6] = d.getQtTrois().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][7] = d.getRemiseQtTrois().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
            }

            for (int i = itemFamilyDiscountList.size(); i < itemFamilyDiscountList.size() + 5; i++) {
                data[i][0] = null;
                data[i][1] = null;
                data[i][2] = null;
                data[i][3] = null;
                data[i][4] = null;
                data[i][5] = null;
                data[i][6] = null;
                data[i][7] = null;
            }

            getFamilyItemDiscountTable().setData(data);
        } else {
            itemFamilyDiscountList = null;
            getFamilyItemDiscountTable().setData(getDiscountEmptyTable());
        }
        setFamilyColumn(getFamilyItemDiscountTable().getTable(), getFamilyItemDiscountTable().getTable().getColumnModel().getColumn(0));
        getFamilyItemDiscountTable().getTable().addMouseListener(new MouseClickFamilyDiscountList());
        getFamilyItemDiscountTable().getTableModel().addTableModelListener(new TableModelListener() {
            public void tableChanged(TableModelEvent e) {
                if (updateRowsTable) {
                    actionUpdateItemFamilyDiscountRow(e.getFirstRow());
                }
            }
        });
    }

    private void actionUpdateItemDiscountRow(int row, int col) {
        if (itemDiscountList == null || row >= itemDiscountList.size()) {
            Integer pk = (Integer) crud.create(48, null);
            if (pk != null) {
                Object data[][] = getItemDiscountTable().getData();
                Discount discount = (Discount) crud.read(pk, 48);
                discount.setClientId(customer.getId());
                discount.setArticleId((String) data[row][0]);
                if (data[row][1] == null) {
                    discount.setRemise(new BigDecimal("0.00"));
                } else {
                    discount.setRemise((BigDecimal) data[row][1]);
                }
                if (data[row][2] == null) {
                    discount.setQtUn(new BigDecimal("0.00"));
                } else {
                    discount.setQtUn((BigDecimal) data[row][2]);
                }
                if (data[row][3] == null) {
                    discount.setRemiseQtUn(new BigDecimal("0.00"));
                } else {
                    discount.setRemiseQtUn((BigDecimal) data[row][3]);
                }
                if (data[row][4] == null) {
                    discount.setQtDeux(new BigDecimal("0.00"));
                } else {
                    discount.setQtDeux((BigDecimal) data[row][4]);
                }
                if (data[row][5] == null) {
                    discount.setRemiseQtDeux(new BigDecimal("0.00"));
                } else {
                    discount.setRemiseQtDeux((BigDecimal) data[row][5]);
                }
                if (data[row][6] == null) {
                    discount.setQtTrois(new BigDecimal("0.00"));
                } else {
                    discount.setQtTrois((BigDecimal) data[row][6]);
                }
                if (data[row][7] == null) {
                    discount.setRemiseQtTrois(new BigDecimal("0.00"));
                } else {
                    discount.setRemiseQtTrois((BigDecimal) data[row][7]);
                }
                discount.setUpdateUserId(param.getUserId());
                crud.update(discount, 48);
            }
        } else {
            Object data[][] = getItemDiscountTable().getData();
            Discount discount = (Discount) itemDiscountList.get(row);
            discount.setClientId(customer.getId());
            discount.setArticleId((String) data[row][0]);
            if (data[row][1] == null) {
                discount.setRemise(new BigDecimal("0.00"));
            } else if (getItemDiscountTable().getTable().getSelectedColumn() == 1 && isCalculatorSelected()) {
                discount.setRemise(actionCalculateDiscount((BigDecimal) data[row][1], (String) data[row][0]));
            } else {
                discount.setRemise((BigDecimal) data[row][1]);
            }
            if (data[row][2] == null) {
                discount.setQtUn(new BigDecimal("0.00"));
            } else {
                discount.setQtUn((BigDecimal) data[row][2]);
            }
            if (data[row][3] == null) {
                discount.setRemiseQtUn(new BigDecimal("0.00"));
            } else if (getItemDiscountTable().getTable().getSelectedColumn() == 3 && isCalculatorSelected()) {
                discount.setRemiseQtUn(actionCalculateDiscount((BigDecimal) data[row][3], (String) data[row][0]));
            } else {
                discount.setRemiseQtUn((BigDecimal) data[row][3]);
            }
            if (data[row][4] == null) {
                discount.setQtDeux(new BigDecimal("0.00"));
            } else {
                discount.setQtDeux((BigDecimal) data[row][4]);
            }
            if (data[row][5] == null) {
                discount.setRemiseQtDeux(new BigDecimal("0.00"));
            } else if (getItemDiscountTable().getTable().getSelectedColumn() == 5 && isCalculatorSelected()) {
                discount.setRemiseQtDeux(actionCalculateDiscount((BigDecimal) data[row][5], (String) data[row][0]));
            } else {
                discount.setRemiseQtDeux((BigDecimal) data[row][5]);
            }
            if (data[row][6] == null) {
                discount.setQtTrois(new BigDecimal("0.00"));
            } else {
                discount.setQtTrois((BigDecimal) data[row][6]);
            }
            if (data[row][7] == null) {
                discount.setRemiseQtTrois(new BigDecimal("0.00"));
            } else if (getItemDiscountTable().getTable().getSelectedColumn() == 7 && isCalculatorSelected()) {
                discount.setRemiseQtTrois(actionCalculateDiscount((BigDecimal) data[row][7], (String) data[row][0]));
            } else {
                discount.setRemiseQtTrois((BigDecimal) data[row][7]);
            }
            discount.setUpdateUserId(param.getUserId());
            crud.update(discount, 48);
        }
        actionLoadItemDiscount();
    }

    private BigDecimal actionCalculateDiscount(BigDecimal netPrice, String itemId) {
        if (itemId == null || netPrice == null) {
            return new BigDecimal("0.00");
        }
        Item item = (Item) crud.read(itemId, 11);
        if (item == null) {
            JOptionPane.showMessageDialog(null, "La R\351f\351rence " + itemId + "n'existe pas !");
            return new BigDecimal("0.00");
        }
        BigDecimal onePercentage = item.getPrixRevient().divide(new BigDecimal("100"), param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
        BigDecimal margeMinimum = onePercentage.multiply(new BigDecimal(param.getMinimumProfitMargin()));
        BigDecimal priceMinimum = item.getPrixRevient().add(margeMinimum).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
        if (netPrice.compareTo(priceMinimum) == -1) {
            JOptionPane.showMessageDialog(null, "Remise refus\351e !\nLa remise est au dessous de la marge minimal autoris\351e\nLe prix net minimum autoris\351 est de " + priceMinimum + "\n" + "(marge minimal autoris\351e " + param.getMinimumProfitMargin() + "%)");
            return new BigDecimal("0.00");
        } else {
            onePercentage = item.getPriceList1().divide(new BigDecimal("100"), param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
            BigDecimal delta = item.getPriceList1().subtract(netPrice);
            BigDecimal rate = delta.divide(onePercentage, param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
            return rate;
        }
    }

    private void actionUpdateItemFamilyDiscountRow(int row) {
        if (itemFamilyDiscountList == null || row >= itemFamilyDiscountList.size()) {
            Integer pk = (Integer) crud.create(49, null);
            if (pk != null) {
                Object data[][] = getFamilyItemDiscountTable().getData();
                Discount discount = (Discount) crud.read(pk, 49);
                discount.setClientId(customer.getId());
                discount.setArticleId((String) data[row][0]);
                if (data[row][1] == null) {
                    discount.setRemise(new BigDecimal("0.00"));
                } else {
                    discount.setRemise((BigDecimal) data[row][1]);
                }
                if (data[row][2] == null) {
                    discount.setQtUn(new BigDecimal("0.00"));
                } else {
                    discount.setQtUn((BigDecimal) data[row][2]);
                }
                if (data[row][3] == null) {
                    discount.setRemiseQtUn(new BigDecimal("0.00"));
                } else {
                    discount.setRemiseQtUn((BigDecimal) data[row][3]);
                }
                if (data[row][4] == null) {
                    discount.setQtDeux(new BigDecimal("0.00"));
                } else {
                    discount.setQtDeux((BigDecimal) data[row][4]);
                }
                if (data[row][5] == null) {
                    discount.setRemiseQtDeux(new BigDecimal("0.00"));
                } else {
                    discount.setRemiseQtDeux((BigDecimal) data[row][5]);
                }
                if (data[row][6] == null) {
                    discount.setQtTrois(new BigDecimal("0.00"));
                } else {
                    discount.setQtTrois((BigDecimal) data[row][6]);
                }
                if (data[row][7] == null) {
                    discount.setRemiseQtTrois(new BigDecimal("0.00"));
                } else {
                    discount.setRemiseQtTrois((BigDecimal) data[row][7]);
                }
                discount.setUpdateUserId(param.getUserId());
                crud.update(discount, 49);
            }
        } else {
            Object data[][] = getFamilyItemDiscountTable().getData();
            Discount discount = (Discount) itemFamilyDiscountList.get(row);
            discount.setClientId(customer.getId());
            discount.setArticleId((String) data[row][0]);
            if (data[row][1] == null) {
                discount.setRemise(new BigDecimal("0.00"));
            } else {
                discount.setRemise((BigDecimal) data[row][1]);
            }
            if (data[row][2] == null) {
                discount.setQtUn(new BigDecimal("0.00"));
            } else {
                discount.setQtUn((BigDecimal) data[row][2]);
            }
            if (data[row][3] == null) {
                discount.setRemiseQtUn(new BigDecimal("0.00"));
            } else {
                discount.setRemiseQtUn((BigDecimal) data[row][3]);
            }
            if (data[row][4] == null) {
                discount.setQtDeux(new BigDecimal("0.00"));
            } else {
                discount.setQtDeux((BigDecimal) data[row][4]);
            }
            if (data[row][5] == null) {
                discount.setRemiseQtDeux(new BigDecimal("0.00"));
            } else {
                discount.setRemiseQtDeux((BigDecimal) data[row][5]);
            }
            if (data[row][6] == null) {
                discount.setQtTrois(new BigDecimal("0.00"));
            } else {
                discount.setQtTrois((BigDecimal) data[row][6]);
            }
            if (data[row][7] == null) {
                discount.setRemiseQtTrois(new BigDecimal("0.00"));
            } else {
                discount.setRemiseQtTrois((BigDecimal) data[row][7]);
            }
            discount.setUpdateUserId(param.getUserId());
            crud.update(discount, 49);
        }
        actionLoadItemFamilyDiscount();
    }

    private void actionUpdate() {
        if (verifyRequirement()) {
            customer.setRaisonSociale(getCompanyValue());
            customer.setCivilite(getStatusValue());
            customer.setAdresseFactUn(getSalesAddrLn1Value());
            customer.setAdresseFactDeux(getSalesAddrLn2Value());
            customer.setVilleFact(getSalesCityValue());
            customer.setPaysFact(getSalesCountryValue());
            customer.setCpFact(getSalesZipValue());
            customer.setNomPrenomContact(getContactValue());
            customer.setFonctionContact(getJobContactValue());
            customer.setTelephone(getTelValue());
            customer.setFax(getFaxValue());
            customer.setAdresseLivrUn(getShippingAddrLn1Value());
            customer.setAdresseLivrDeux(getShippingAddrLn2Value());
            customer.setVilleLivr(getShippingCityValue());
            customer.setCpLivr(getShippingZipValue());
            customer.setPaysLivr(getShippingCountryValue());
            customer.setIdGLN(getGlnValue());
            customer.setEmail(getEmailValue());
            customer.setSiteWeb(getWebValue());
            customer.setPortable(getTel2Value());
            customer.setFamilleClientId(getFamilyValue());
            customer.setNumeroCompte(getBankNumberValue());
            customer.setUserId(param.getUserId());
            customer.setCommentaire(getNotesValue());
            customer.setTotalEncaisser(new BigDecimal(getTotalPayment()));
            customer.setCotaFidelite(new BigDecimal(getExtraDiscountLevel()));
            customer.setRemiseFidelite(new BigDecimal(getExtraDiscount()));
            customer.setRemise(new BigDecimal(getDiscount()));
            SalesPerson sp = (SalesPerson) salesPersonList.get(getSalesPersonIndex());
            customer.setCommerciauxId(sp.getId());
            if (getPaymentDelay().equals("")) {
                customer.setEcheancePaiement(new BigDecimal("0"));
            } else {
                customer.setEcheancePaiement(new BigDecimal(getPaymentDelay()));
            }
            customer.setModeReglement(getPaymentMode());
            crud.update(customer, 27);
            initField();
            initCustomerFamilyList();
            initItemDiscountTable();
            initItemFamilyDiscountTable();
        }
    }

    private void actionCreate() {
        if (verifyRequirement()) {
            String pk = (String) crud.create(27, getIdValue());
            if (pk != null) {
                customer = (Customer) crud.read(pk, 27);
                customer.setRaisonSociale(getCompanyValue());
                customer.setCivilite(getStatusValue());
                customer.setAdresseFactUn(getSalesAddrLn1Value());
                customer.setAdresseFactDeux(getSalesAddrLn2Value());
                customer.setVilleFact(getSalesCityValue());
                customer.setPaysFact(getSalesCountryValue());
                customer.setCpFact(getSalesZipValue());
                customer.setNomPrenomContact(getContactValue());
                customer.setFonctionContact(getJobContactValue());
                customer.setTelephone(getTelValue());
                customer.setFax(getFaxValue());
                customer.setAdresseLivrUn(getShippingAddrLn1Value());
                customer.setAdresseLivrDeux(getShippingAddrLn2Value());
                customer.setVilleLivr(getShippingCityValue());
                customer.setCpLivr(getShippingZipValue());
                customer.setPaysLivr(getShippingCountryValue());
                customer.setIdGLN(getGlnValue());
                customer.setEmail(getEmailValue());
                customer.setSiteWeb(getWebValue());
                customer.setPortable(getTel2Value());
                customer.setFamilleClientId(getFamilyValue());
                customer.setNumeroCompte(getBankNumberValue());
                customer.setUserId(param.getUserId());
                customer.setCommentaire(getNotesValue());
                customer.setTotalEncaisser(new BigDecimal(getTotalPayment()));
                customer.setCotaFidelite(new BigDecimal(getExtraDiscountLevel()));
                customer.setRemiseFidelite(new BigDecimal(getExtraDiscount()));
                customer.setRemise(new BigDecimal(getDiscount()));
                SalesPerson sp = (SalesPerson) salesPersonList.get(getSalesPersonIndex());
                customer.setCommerciauxId(sp.getId());
                customer.setEcheancePaiement(new BigDecimal(getPaymentDelay()));
                customer.setModeReglement(getPaymentMode());
                crud.update(customer, 27);
                initField();
                initCustomerFamilyList();
                initItemDiscountTable();
                initItemFamilyDiscountTable();
            } else {
                JOptionPane.showMessageDialog(null, "Code Client refus\351 !\n(v\351rif\351 si ce code n'est pas d\351j\340 utilis\351)");
            }
        }
    }

    private void actionDelete() {
        crud.delete(customer.getId(), customer.getUpdateLastTime(), 27);
        initField();
        initCustomerFamilyList();
        initItemDiscountTable();
        initItemFamilyDiscountTable();
    }

    private void actionDeleteDiscountLine() {
        if (getSelectedDiscountPanel() == 0) {
            Discount discount = (Discount) itemDiscountList.get(getItemDiscountTable().getTable().getSelectedRow());
            crud.delete(discount.getId(), discount.getUpdateTime(), 48);
            actionLoadItemDiscount();
        } else if (getSelectedDiscountPanel() != -1) {
            Discount discount = (Discount) itemFamilyDiscountList.get(getFamilyItemDiscountTable().getTable().getSelectedRow());
            crud.delete(discount.getId(), discount.getUpdateTime(), 49);
            actionLoadItemFamilyDiscount();
        }
    }

    private boolean verifyRequirement() {
        if (getNotesValue() != null && getNotesValue().length() > 250) {
            JOptionPane.showMessageDialog(null, "Commentaires trop long (maxi. 250 caract\350res) !");
            return false;
        }
        if (getCompanyValue().length() < 2 || getCompanyValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "Raison sociale obligatoire (mini. 2 caract\350res, maxi. 50 caract\350res) !");
            return false;
        }
        if (getSalesCountryValue() != null && getSalesCountryValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "Adresse de facturation Pays trop long (maxi. 50 caract\350res) !");
            return false;
        }
        if (getShippingCountryValue() != null && getShippingCountryValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "Adresse de livraison Pays trop long (maxi. 50 caract\350res) !");
            return false;
        }
        if (getTel2Value() != null && getTel2Value().length() > 20) {
            JOptionPane.showMessageDialog(null, "T\351l\351phone 2 trop long (maxi. 20 caract\350res) !");
            return false;
        }
        if (getEmailValue() != null && getEmailValue().length() > 100) {
            JOptionPane.showMessageDialog(null, "Email trop long (maxi. 100 caract\350res) !");
            return false;
        }
        if (getFaxValue() != null && getFaxValue().length() > 20) {
            JOptionPane.showMessageDialog(null, "Fax trop long (maxi. 20 caract\350res) !");
            return false;
        }
        if (getTelValue() != null && getTelValue().length() > 20) {
            JOptionPane.showMessageDialog(null, "T\351l\351phone trop long (maxi. 20 caract\350res) !");
            return false;
        }
        if (getSalesZipValue() != null && getSalesZipValue().length() > 20) {
            JOptionPane.showMessageDialog(null, "Adresse de facturation Code postal trop long (maxi. 20 caract\350res) !");
            return false;
        }
        if (getSalesCityValue() != null && getSalesCityValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "Adresse de facturation Ville trop longue (maxi. 50 caract\350res) !");
            return false;
        }
        if (getSalesAddrLn2Value() != null && getSalesAddrLn2Value().length() > 100) {
            JOptionPane.showMessageDialog(null, "Adresse de facturation ligne 2 trop longue (maxi. 100 caract\350res) !");
            return false;
        }
        if (getSalesAddrLn1Value() != null && getSalesAddrLn1Value().length() > 100) {
            JOptionPane.showMessageDialog(null, "Adresse de facturation ligne 1 trop longue (maxi. 100 caract\350res) !");
            return false;
        }
        if (getShippingZipValue() != null && getShippingZipValue().length() > 20) {
            JOptionPane.showMessageDialog(null, "Adresse de livraison Code postal trop long (maxi. 20 caract\350res) !");
            return false;
        }
        if (getShippingCityValue() != null && getShippingCityValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "Adresse de livraison Ville trop longue (maxi. 50 caract\350res) !");
            return false;
        }
        if (getShippingAddrLn2Value() != null && getShippingAddrLn2Value().length() > 100) {
            JOptionPane.showMessageDialog(null, "Adresse de livraison ligne 2 trop longue (maxi. 100 caract\350res) !");
            return false;
        }
        if (getShippingAddrLn1Value() != null && getShippingAddrLn1Value().length() > 100) {
            JOptionPane.showMessageDialog(null, "Adresse de livraison ligne 1 trop longue (maxi. 100 caract\350res) !");
            return false;
        }
        if (getContactValue() != null && getContactValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "Contact trop long (maxi. 50 caract\350res) !");
            return false;
        }
        if (getJobContactValue() != null && getJobContactValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "Fonction contact trop longue (maxi. 50 caract\350res) !");
            return false;
        }
        if (getGlnValue() != null && getGlnValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "GLN trop long (maxi. 50 caract\350res) !");
            return false;
        }
        if (getBankNumberValue() != null && getBankNumberValue().length() > 50) {
            JOptionPane.showMessageDialog(null, "N\260 compte trop long (maxi. 50 caract\350res) !");
            return false;
        }
        if (getWebValue() != null && getWebValue().length() > 100) {
            JOptionPane.showMessageDialog(null, "Site web trop long (maxi. 100 caract\350res) !");
            return false;
        }
        try {
            new BigDecimal(getPaymentDelay());
        } catch (NumberFormatException e) {
            setPaymentDelay("0");
        }
        try {
            new BigDecimal(getExtraDiscountLevel());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Montant quota fid\350lit\351 refus\351 (nombre d\351cimal) !");
            return false;
        }
        try {
            new BigDecimal(getDiscount());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Montant remise refus\351e (nombre d\351cimal) !");
            return false;
        }
        try {
            new BigDecimal(getExtraDiscount());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Montant remise exceptionelle refus\351e (nombre d\351cimal) !");
            return false;
        }
        return true;
    }
    private ObjectCRUD crud;
    private CustomerCRUD customerCrud;
    private ItemCRUD itemCrud;
    private Parameter param;
    private boolean create;
    private boolean updateRowsTable;
    private Customer customer;
    private TableViewer customerTable;
    private TableViewer itemTable;
    private HashMap salesPersonIdList;
    private ArrayList salesPersonList;
    private ArrayList itemDiscountList;
    private ArrayList itemFamilyDiscountList;
    private PopupDiscountGUI popupDiscount;
    private boolean write;
}
